﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Southridge.Data;
 

namespace Southridge.View
{
    public class PreferencesEditorViewModel  : ViewModelBase 
    {
        public PreferencesEditorViewModel ( Preference preference) 
        { 
           _preference = preference;
           _preference.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_preference_PropertyChanged);
        }

        void _preference_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PropertyType")
            {
                if (  ((_preference.PropertyType & PropertyType.SingleFamily)== PropertyType.SingleFamily) != IsSingleFamilySelected ) 
                    OnPropertyChanged("IsSingleFamilySelected") ;

                if (  ((_preference.PropertyType & PropertyType.MultiFamily)== PropertyType.MultiFamily) != IsMultiFamilySelected ) 
                    OnPropertyChanged("IsMultiFamilySelected") ;

                if (  ((_preference.PropertyType & PropertyType.Condominium)== PropertyType.Condominium) != IsCondominiumSelected ) 
                    OnPropertyChanged("IsCondominiumSelected") ;
            }
            if (e.PropertyName == "State")
            {
                OnPropertyChanged("IsCountyEnabled");
                OnPropertyChanged("IsNeighborhoodEnabled"); 
            }

            if (e.PropertyName == "County")
            {
                OnPropertyChanged("IsNeighborhoodEnabled"); 
            }
             
        }

        public bool IsCountyEnabled
        {
            get
            {
                return Preference.State != null; 
            } 
        }

        public bool IsNeighborhoodEnabled
        {
            get
            {
                return Preference.County != null;
            }
        } 

        Preference _preference;  
        public Preference Preference
        {
            get
            {
                return _preference; 
            }
        }

        public bool IsSingleFamilySelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.SingleFamily) == PropertyType.SingleFamily ) ; 
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.SingleFamily;
                else
                    Preference.PropertyType = (int) Preference.PropertyType - (PropertyType.SingleFamily);

                OnPropertyChanged("IsSingleFamilySelected");  
            }
        }

        public bool IsMultiFamilySelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.MultiFamily) == PropertyType.MultiFamily);
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.MultiFamily;
                else
                    Preference.PropertyType = (int)Preference.PropertyType - (PropertyType.MultiFamily);

                OnPropertyChanged("IsMultiFamilySelected");
            }
        }

        public bool IsCondominiumSelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.Condominium) == PropertyType.Condominium);
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.Condominium;
                else
                    Preference.PropertyType = (int)Preference.PropertyType - (PropertyType.Condominium);

                OnPropertyChanged("IsCondominiumSelected");
            }
        }

        public bool IsTownHomeSelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.TownHome) == PropertyType.TownHome);
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.TownHome;
                else
                    Preference.PropertyType = (int)Preference.PropertyType - (PropertyType.TownHome);

                OnPropertyChanged("IsTownHomeSelected");
            }
        }

        public bool IsLandSelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.Land) == PropertyType.Land);
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.Land;
                else
                    Preference.PropertyType = (int)Preference.PropertyType - (PropertyType.Land);

                OnPropertyChanged("IsLandSelected");
            }
        }

        public bool IsCommercialSelected
        {
            get
            {
                return ((Preference.PropertyType & PropertyType.Commercial) == PropertyType.Commercial);
            }
            set
            {
                if (value)
                    Preference.PropertyType = Preference.PropertyType | PropertyType.Commercial;
                else
                    Preference.PropertyType = (int)Preference.PropertyType - (PropertyType.Commercial);

                OnPropertyChanged("IsCommercialSelected");
            }
        } 
    }
}
